<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryRef"
      :inline="true"
      v-show="showSearch"
      label-width="90px"
    >
      <el-form-item label="题目名字" prop="questionName">
        <el-input
          v-model="queryParams.questionName"
          placeholder="请输入题目名字"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="问题分类" prop="questionType">
        <el-select
          v-model="queryParams.questionType"
          placeholder="问题分类"
          @change="handleQuery"
        >
          <el-option
            v-for="item in questionTypeList"
            :key="item.dictCode"
            :label="item.dictLabel"
            :value="item.dictValue"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="问题难易度" prop="questionLevelId">
        <el-select
          v-model="queryParams.questionLevelId"
          placeholder="问题难易度"
          @change="handleQuery"
        >
          <el-option
            v-for="item in questionLevelList"
            :key="item.dictCode"
            :label="item.dictLabel"
            :value="item.dictValue"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="创建时间" style="width: 308px">
        <el-date-picker
          v-model="daterangeCreateTime"
          value-format="YYYY-MM-DD"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" :icon="Search" @click="handleQuery"
          >搜索</el-button
        >
        <el-button :icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <div class="all-button">
        <div class="left-button">
          <el-col :span="1.5">
            <el-button
              type="primary"
              plain
              :icon="Plus"
              @click="handleAdd"
              v-hasPerm="['examManage:question:add']"
              >新增</el-button
            >
          </el-col>
          <el-col :span="1.5">
            <el-button
              type="success"
              plain
              :icon="Edit"
              :disabled="single"
              @click="handleUpdate"
              v-hasPerm="['examManage:question:edit']"
              >修改</el-button
            >
          </el-col>
          <el-col :span="1.5">
            <el-button
              type="danger"
              plain
              :icon="Delete"
              :disabled="multiple"
              @click="handleDelete"
              v-hasPerm="['examManage:question:remove']"
              >删除</el-button
            >
          </el-col>
          <el-col :span="1.5">
            <el-button
              type="warning"
              plain
              :icon="Download"
              @click="handleExport"
              v-hasPerm="['examManage:question:export']"
              >导出</el-button
            >
          </el-col>
        </div>
        <div>
          <right-toolbar
            v-model:showSearch="showSearch"
            @queryTable="getList"
          ></right-toolbar>
        </div>
      </div>
    </el-row>

    <el-table
      v-loading="loading"
      :data="questionList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="编号" align="center" prop="questionId" />
      <el-table-column label="题目名字" align="center" prop="questionName" />
      <el-table-column label="题目分数" align="center" prop="questionScore" />
      <el-table-column
        label="问题类型"
        align="center"
        prop="questionTypeLabel"
      />
      <el-table-column
        label="难易度"
        align="center"
        prop="questionLevelLabel"
      />
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template #default="scope">
          <el-tooltip content="编辑" placement="top">
            <el-button
              link
              type="primary"
              :icon="Edit"
              @click="handleUpdate(scope.row)"
              v-hasPerm="['examManage:question:edit']"
            ></el-button>
          </el-tooltip>
          <el-tooltip content="删除" placement="top">
            <el-button
              link
              type="primary"
              :icon="Delete"
              @click="handleDelete(scope.row)"
              v-hasPerm="['examManage:question:remove']"
            ></el-button>
          </el-tooltip>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改考试题目对话框 -->
    <el-dialog :title="title" v-model="open" width="60%" append-to-body>
      <el-form
        ref="questionRef"
        :model="form"
        :rules="rules"
        label-width="120px"
      >
        <el-row>
          <el-col :span="12">
            <el-form-item label="题目名字" prop="questionName">
              <el-input
                v-model="form.questionName"
                placeholder="请输入题目名字"
                maxlength="120"
                show-word-limit
              /> </el-form-item
          ></el-col>
          <el-col :span="12">
            <el-form-item label="题目分数" prop="questionScore">
              <el-input-number
                v-model="form.questionScore"
                :min="1"
                :max="100"
              />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="问题分类" prop="questionType">
              <el-select v-model="form.questionType" placeholder="问题分类">
                <el-option
                  v-for="item in questionTypeList"
                  :key="item.dictCode"
                  :label="item.dictLabel"
                  :value="item.dictValue"
                >
                </el-option>
              </el-select> </el-form-item
          ></el-col>
          <el-col :span="12">
            <el-form-item label="问题难易度" prop="questionLevelId">
              <el-select
                v-model="form.questionLevelId"
                placeholder="问题难易度"
              >
                <el-option
                  v-for="item in questionLevelList"
                  :key="item.dictCode"
                  :label="item.dictLabel"
                  :value="item.dictValue"
                >
                </el-option>
              </el-select> </el-form-item
          ></el-col>
        </el-row>
        <el-row v-if="form.questionType == 'judge'">
          <el-col :span="12">
            <el-form-item label="选项A" prop="questionOptionA">
              <el-input
                v-model="form.questionOptionA"
                placeholder="请输入选项A"
                maxlength="120"
                show-word-limit
              /> </el-form-item
          ></el-col>
          <el-col :span="12"
            ><el-form-item label="选项B" prop="questionOptionB">
              <el-input
                v-model="form.questionOptionB"
                placeholder="请输入选项B"
                maxlength="120"
                show-word-limit
              /> </el-form-item
          ></el-col>
        </el-row>
        <el-row
          v-if="form.questionType == 'single' || form.questionType == 'multi'"
        >
          <el-col :span="12">
            <el-form-item label="选项A" prop="questionOptionA">
              <el-input
                v-model="form.questionOptionA"
                placeholder="请输入选项A"
                maxlength="120"
                show-word-limit
              /> </el-form-item
          ></el-col>
          <el-col :span="12"
            ><el-form-item label="选项B" prop="questionOptionB">
              <el-input
                v-model="form.questionOptionB"
                placeholder="请输入选项B"
                maxlength="120"
                show-word-limit
              /> </el-form-item
          ></el-col>
        </el-row>
        <el-row
          v-if="form.questionType == 'single' || form.questionType == 'multi'"
        >
          <el-col :span="12">
            <el-form-item label="选项C" prop="questionOptionC">
              <el-input
                v-model="form.questionOptionC"
                placeholder="请输入选项C"
                maxlength="120"
                show-word-limit
              /> </el-form-item
          ></el-col>
          <el-col :span="12">
            <el-form-item label="选项D" prop="questionOptionD">
              <el-input
                v-model="form.questionOptionD"
                placeholder="请输入选项D"
                maxlength="120"
                show-word-limit
              /> </el-form-item
          ></el-col>
        </el-row>

        <el-form-item label="正确答案" prop="answer">
          <el-input
            v-model="form.answer"
            type="textarea"
            placeholder="请输入内容"
            :rows="4"
            maxlength="1000"
            show-word-limit
          />
        </el-form-item>
        <el-form-item label="题目解析" prop="analysis">
          <el-input
            v-model="form.analysis"
            type="textarea"
            placeholder="请输入内容"
            :rows="4"
            maxlength="1000"
            show-word-limit
          />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input
            v-model="form.remark"
            type="textarea"
            placeholder="请输入内容"
            maxlength="300"
            show-word-limit
          />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Question">
import {
  listQuestion,
  getQuestion,
  delQuestion,
  addQuestion,
  updateQuestion,
} from "@/api/examManage/question";
import { ref, reactive, toRefs, getCurrentInstance } from "vue";
import {
  Search,
  Plus,
  Refresh,
  Edit,
  Download,
  Delete,
} from "@element-plus/icons-vue";
import { listDictData } from "@/api/sys/dictData";

const { proxy } = getCurrentInstance();
const questionLevelList = ref([]);
const questionList = ref([]);
const questionTypeList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");
const daterangeCreateTime = ref([]);

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    questionName: null,
    questionType: null,
    questionLevelId: null,
    createTime: null,
  },
  rules: {
    questionName: [
      { required: true, message: "题目名字不能为空", trigger: "blur" },
    ],
    questionScore: [
      { required: true, message: "题目分数不能为空", trigger: "blur" },
    ],
    questionType: [
      { required: true, message: "问题类型不能为空", trigger: "change" },
    ],
    questionLevelId: [
      { required: true, message: "难易度不能为空", trigger: "change" },
    ],
    answer: [{ required: true, message: "正确答案不能为空", trigger: "blur" }],
  },
});

const { queryParams, form, rules } = toRefs(data);

listDictData({
  pageNum: 1,
  pageSize: 20,
  dictType: "question_level",
}).then((res) => {
  questionLevelList.value = res.data.rows;
});
listDictData({
  pageNum: 1,
  pageSize: 20,
  dictType: "question_type",
}).then((res) => {
  questionTypeList.value = res.data.rows;
});
/** 查询考试题目列表 */
function getList() {
  loading.value = true;
  queryParams.value.params = {};
  if (null != daterangeCreateTime && "" != daterangeCreateTime) {
    queryParams.value.params["beginCreateTime"] = daterangeCreateTime.value[0];
    queryParams.value.params["endCreateTime"] = daterangeCreateTime.value[1];
  }
  listQuestion(queryParams.value).then((response) => {
    response.data.rows.map((t) => {
      t.questionLevelLabel = "";
      t.questionTypeLabel = "";
      let obj = questionLevelList.value.find(
        (m) => m.dictValue == t.questionLevelId
      );
      if (obj) {
        t.questionLevelLabel = obj.dictLabel;
      }
      let _obj = questionTypeList.value.find(
        (m) => m.dictValue == t.questionType
      );
      if (_obj) {
        t.questionTypeLabel = _obj.dictLabel;
      }
      return t;
    });

    questionList.value = response.data.rows;
    total.value = response.data.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    questionId: null,
    questionName: null,
    questionScore: null,
    questionType: null,
    questionLevelId: null,
    questionOptionA: null,
    questionOptionB: null,
    questionOptionC: null,
    questionOptionD: null,
    answer: null,
    analysis: null,
    createTime: null,
    updateTime: null,
    createBy: null,
    updateBy: null,
    remark: null,
  };
  proxy.resetForm("questionRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  daterangeCreateTime.value = [];
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map((item) => item.questionId);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加考试题目";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _questionId = row.questionId || ids.value;
  getQuestion(_questionId).then((response) => {
    form.value = response.data.data;
    open.value = true;
    title.value = "修改考试题目";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["questionRef"].validate((valid) => {
    if (valid) {
      if (form.value.questionId != null) {
        updateQuestion(form.value).then((response) => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addQuestion(form.value).then((response) => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _questionIds = row.questionId || ids.value;
  proxy.$modal
    .confirm('是否确认删除考试题目编号为"' + _questionIds + '"的数据项？')
    .then(function () {
      return delQuestion(_questionIds);
    })
    .then(() => {
      getList();
      proxy.$modal.msgSuccess("删除成功");
    })
    .catch(() => {});
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download(
    "examManage/question/export",
    {
      ...queryParams.value,
    },
    `question_${new Date().getTime()}.xlsx`
  );
}

getList();
</script>
